package com.gitee.l0km.xthrift.base.metadata;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;

import javax.annotation.Nullable;

import com.gitee.l0km.aocache.annotations.AoWeakCacheable;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

public class FieldNameFilters{
	private final Map<Class<?>, List<String>> excludeFields;

	public FieldNameFilters(Map<Class<?>, List<String>> excludeFields) {
		super();
		this.excludeFields = excludeFields;
	}
	@AoWeakCacheable(expireAfterAccess = 60, expireAfterAccessTimeUnit = TimeUnit.SECONDS)
	public Predicate<String> filterOf(Class<?> structClass){
		for(Entry<Class<?>, List<String>> entry:excludeFields.entrySet()){
			if(entry.getKey().isAssignableFrom(structClass)){
				return new Filter(entry.getValue());
			}
		}
		return Predicates.alwaysTrue();
}

	private class Filter implements Predicate<String>{
		final List<String> excludeFields;
		
		public Filter(List<String>excludeFields) {
			super();
			this.excludeFields = excludeFields;
		}

		@Override
		public boolean apply( @Nullable String input) {
			return !excludeFields.contains(input);
		}
		
	}

	/**
	 * @return excludeFields
	 */
	public Map<Class<?>, List<String>> getExcludeFields() {
		return excludeFields;
	}
}
